package controller;

import net.sf.json.JSONObject;
import pojo.User;
import service.LoginManagement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: zen
 * @date: 2020/4/12
 * @Title: registryCheck
 * @Description: 注册检查
 */
@WebServlet("/registry")
public class RegistryCheck extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        String userInfo = req.getParameter("user");
        JSONObject jsonObject = JSONObject.fromObject(userInfo);
        User user = (User) JSONObject.toBean(jsonObject, User.class);
        user.setRole(0);
        user.setAge(0);
        if (LoginManagement.registry(user)) {
            out.write("ok");
        } else {
            out.write("error");
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
